<!--Tomcat信息-->
<template>
  <div class="app-container">
    <!--列表-->
    <div class="table-container flex-main">
      <div class="table-container-header">
        <el-alert
          type="success"
          show-icon
          :closable="false">
          <div slot="title">
            上次更新时间：{{ updateTime | formatDate(T.DATE_TIME_FMT1) }}
            <el-divider direction="vertical" />
            <el-link type="primary" :underline="false" @click="handleClickUpdate">立即更新</el-link>
          </div>
        </el-alert>
      </div>
      <el-table
        ref="multipleTable"
        :data="dataSource"
        row-key="id"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row>
        <el-table-column label="参数" prop="param" align="left" min-width="200">
          <template slot-scope="scope">
            <span :class="'color-'+textInfo[scope.row.param].color">{{ scope.row.param }}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="text" align="left" min-width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ textInfo[scope.row.param].text }}
          </template>
        </el-table-column>
        <el-table-column align="left" prop="value" label="当前值" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.value }} {{ textInfo[scope.row.param].unit }}
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
</template>
<script>
export default {
  data() {
    return {
      updateTime: new Date().getTime(),
      dataSource: [],
      moreInfo: {
        'tomcat.global.request': ['.count', '.totalTime'],
        'tomcat.servlet.request': ['.count', '.totalTime']
      },
      textInfo: {
        'tomcat.sessions.created': { color: 'success', text: 'tomcat 已创建 session 数', unit: '个' },
        'tomcat.sessions.expired': { color: 'success', text: 'tomcat 已过期 session 数', unit: '个' },
        'tomcat.sessions.active.current': { color: 'success', text: 'tomcat 当前活跃 session 数', unit: '个' },
        'tomcat.sessions.active.max': { color: 'success', text: 'tomcat 活跃 session 数峰值', unit: '个' },
        'tomcat.sessions.rejected': { color: 'success', text: '超过session 最大配置后，拒绝的 session 个数', unit: '个' },

        'tomcat.global.sent': { color: 'primary', text: '发送的字节数', unit: 'bytes' },
        'tomcat.global.request.max': { color: 'primary', text: 'request 请求最长耗时', unit: '秒' },
        'tomcat.global.request.count': { color: 'primary', text: '全局 request 请求次数', unit: '次' },
        'tomcat.global.request.totalTime': { color: 'primary', text: '全局 request 请求总耗时', unit: '秒' },

        'tomcat.servlet.request.max': { color: 'warning', text: 'servlet 请求最长耗时', unit: '秒' },
        'tomcat.servlet.request.count': { color: 'warning', text: 'servlet 总请求次数', unit: '次' },
        'tomcat.servlet.request.totalTime': { color: 'warning', text: 'servlet 请求总耗时', unit: '秒' },

        'tomcat.threads.current': { color: 'danger', text: 'tomcat 当前线程数（包括守护线程）', unit: '个' },
        'tomcat.threads.config.max': { color: 'danger', text: 'tomcat 配置的线程最大数', unit: '个' }
      }
    }
  },
  mounted() {
    this.loadTomcatInfo()
  },
  methods: {
    handleClickUpdate() {
      this.loadTomcatInfo()
    },
    loadTomcatInfo() {
      this.updateTime = new Date().getTime()
      this.dataSource = []
      this.$store.dispatch('monitorTomcatInfo').then(res => {
        const tomcatInfo = []
        res.forEach((value, id) => {
          let more = this.moreInfo[value.name]
          if (!(more instanceof Array)) {
            more = ['']
          }
          more.forEach((item, idx) => {
            const param = value.name + item
            tomcatInfo.push({
              id: param + id, param,
              text: 'false value',
              value: value.measurements[idx].value
            })
          })
        })
        this.dataSource = tomcatInfo
      }).catch((_) => {})
    },
    convert(value, type) {
      if (type === Number) {
        return Number(value * 100).toFixed(2)
      } else if (type === Date) {
        return this.T.formatDate(value * 1000, 'yyyy-MM-dd hh:mm:ss')
      }
      return value
    }
  }

}
</script>
